﻿using UnityEngine;
using UnityEditor;
using System.IO;
using System.Collections.Generic;
using SoarD.Pandora;
using SoarDEditor.Pandora;

public class PdrAtlasMaker
{
    //static string PATH_ATLAS = PdrConfig.Instance.SourceAtlasPath;
    //static string PATH_TP_TEMP = PdrConfig.Instance.TempTPPath;
    //static string PATH_ATLAS_TP = PdrConfig.Instance.AtlasTPPath;

    //#region 对外接口
    //public static void MakeArtAtlas(Object obj, bool isSplitChannel, bool isUI)
    //{
    //    string sorPath = AssetDatabase.GetAssetPath(obj);
    //    string name = Path.GetFileNameWithoutExtension(sorPath);
    //    ExportArtAtlas(sorPath, name, isSplitChannel, isUI);
    //}
    //public static void MakePolyAtlas(Object obj, bool isCompress, bool isSplitChannel)
    //{
    //    string sorPath = AssetDatabase.GetAssetPath(obj);
    //    string name = Path.GetFileNameWithoutExtension(sorPath);
    //    ExportPolyAtlas(sorPath, name, isCompress, isSplitChannel);
    //}
    //public static void MakeAtlas(Object obj, bool isCompress, bool isSplitChannel)
    //{
    //    string sorPath = AssetDatabase.GetAssetPath(obj);
    //    string name = Path.GetFileNameWithoutExtension(sorPath);
    //    ExportAtlas(sorPath, name, isCompress, isSplitChannel);
    //}
    //public static void MakeOnlyTP(Object obj)
    //{
    //    string sorPath = AssetDatabase.GetAssetPath(obj);
    //    string name = Path.GetFileNameWithoutExtension(sorPath);
    //    ExportOnlyTP(sorPath, name);
    //}
    //public static void MakeAtlasByStep(string path, AtlasEditorHelper.Step step)
    //{
    //    if (File.Exists(path))
    //    {
    //        int index = path.LastIndexOf("/");
    //        string name = path.Substring(index + 1);
    //        name = name.Replace(".png", "");
    //        string p = path.Substring(0, index);
    //        AtlasEditorHelper.Build(step, name, p, p, p, false);
    //    }
    //    else
    //    {
    //        DirectoryInfo root = new DirectoryInfo(path);
    //        FileInfo[] files = root.GetFiles();
    //        foreach (FileInfo info in files)
    //        {
    //            string name = info.Name;
    //            name = name.Replace(".png", "");
    //            AtlasEditorHelper.Build(step, name, path, path, path, false);
    //        }
    //    }
    //    Debug.Log(System.Enum.GetName(typeof(AtlasEditorHelper.Step), step) + " Completed");
    //}
    //public static void MakeSpineSplitSprite(string path)
    //{
    //    OptimizingSpineResource(path);
    //}
    //public static void MakeSpriteAnimation(string name, string pathSrc, string pathTP, string pathDst)
    //{
    //    BindTpye();
    //    bool trim = true;
    //    bool isCompress = true;
    //    bool isSplitChannel = true;
    //    bool isUI = false;
    //    AtlasEditorHelper.Build(AtlasEditorHelper.Step.ALL, name, pathSrc, pathTP, pathDst, trim, isCompress, isSplitChannel, isUI);
    //}
    //public static void MakeSceneSprite(string name, string pathSrc, string pathTP, string pathDst)
    //{
    //    BindTpye();
    //    bool trim = true;
    //    bool isCompress = true;
    //    bool isSplitChannel = true;
    //    bool isUI = false;
    //    AtlasEditorHelper.Build(AtlasEditorHelper.Step.ALL, name, pathSrc, pathTP, pathDst, trim, isCompress, isSplitChannel, isUI);
    //}
    //public static void MakeAllAtlas()
    //{
    //    BindTpye();
    //    DirectoryInfo root = new DirectoryInfo(PATH_ATLAS);
    //    int count = root.GetDirectories().Length;
    //    int index = 0;
    //    foreach (DirectoryInfo atlas in root.GetDirectories())
    //    {
    //        index++;
    //        ExportAtlasWithoutProgress(PATH_ATLAS + "/" + atlas.Name, atlas.Name);
    //        AtlasEditorHelper.PlayProgressBar(index + "/" + count, atlas.Name, (float)index / (float)count);
    //    }
    //    AtlasEditorHelper.ClearProgressBar();
    //    Debug.Log("Build All Atlas Completed");
    //}
    //#endregion
    //private static void ExportOnlyTP(string path, string name)
    //{
    //    BindTpye();

    //    int totalCount = 1;
    //    int current = 0;

    //    bool trim = true;
    //    bool isCompress = true;
    //    bool isSplitChannel = false;
    //    bool isUI = false;

    //    AtlasEditorHelper.PlayProgressBar(name, "MAKE_ATLAS", (++current) / totalCount);
    //    AtlasEditorHelper.Build(AtlasEditorHelper.Step.MAKE_ATLAS, name, path, path, path, trim, isCompress, isSplitChannel, isUI);

    //    AtlasEditorHelper.ClearProgressBar();

    //}

    //private static void ExportArtAtlas(string path, string name, bool isSplitChannel = true, bool isUI = true)
    //{
    //    BindTpye();
    //    bool trim = true;
    //    bool isCompress = true;
    //    int totalCount = 3;
    //    int current = 0;
    //    string pathDst = path + "/" + name;
    //    AtlasEditorHelper.PlayProgressBar(name, "MAKE_ATLAS", (++current) / totalCount);
    //    AtlasEditorHelper.Build(AtlasEditorHelper.Step.MAKE_ATLAS, name, path, PATH_TP_TEMP, pathDst, trim, isCompress, isSplitChannel, isUI);

    //    AtlasEditorHelper.PlayProgressBar(name, "SPLIT_CHANNEL", (++current) / totalCount);
    //    AtlasEditorHelper.Build(AtlasEditorHelper.Step.SPLIT_CHANNEL, name, path, PATH_TP_TEMP, pathDst, trim, isCompress, isSplitChannel, isUI);

    //    AtlasEditorHelper.PlayProgressBar(name, "SPLIT_CHANNEL", (++current) / totalCount);
    //    AtlasEditorHelper.Build(AtlasEditorHelper.Step.CREATE_MATERIAL, name, path, PATH_TP_TEMP, pathDst, trim, isCompress, isSplitChannel, isUI);

    //    AtlasEditorHelper.PlayProgressBar(name, "IMPORT_SPRITE", (++current) / totalCount);
    //    AtlasEditorHelper.Build(AtlasEditorHelper.Step.IMPORT_SPRITE, name, path, PATH_TP_TEMP, pathDst, trim, isCompress, isSplitChannel, isUI);

    //    AtlasEditorHelper.ClearProgressBar();

    //}
    //private static void ExportPolyAtlas(string path, string name, bool isCompress = true, bool isSplitChannel = true){
    //    if ((!Config.IsTR && path.Contains("Data_oe")) || (Config.IsTR && !path.Contains("Data_oe")))
    //    {   
    //        Debug.LogError("打图集错误, 尝试把TR(DK)图集打到DK(TR)里,请切换DK/TR工程, 再打图集");
    //        return;
    //    }
    //    BindTpye();

    //    bool trim = true;

    //    int totalCount = 5;
    //    int current = 0;

    //    AtlasEditorHelper.PlayProgressBar(name, "MAKE_POLY_ATLAS", (++current) / totalCount);
    //    AtlasEditorHelper.Build(AtlasEditorHelper.Step.MAKE_POLY_ATLAS, name, path, PATH_TP_TEMP, PATH_ATLAS_TP, trim, isCompress, isSplitChannel);

    //    AtlasEditorHelper.PlayProgressBar(name, "SPLIT_CHANNEL_POLY", (++current) / totalCount);
    //    AtlasEditorHelper.Build(AtlasEditorHelper.Step.SPLIT_CHANNEL_POLY, name, path, PATH_TP_TEMP, PATH_ATLAS_TP, trim, isCompress, isSplitChannel);

    //    AtlasEditorHelper.PlayProgressBar(name, "CREATE_MATERIAL", (++current) / totalCount);
    //    AtlasEditorHelper.Build(AtlasEditorHelper.Step.CREATE_MATERIAL, name, path, PATH_TP_TEMP, PATH_ATLAS_TP, trim, isCompress, isSplitChannel);

    //    AtlasEditorHelper.PlayProgressBar(name, "IMPORT_POLY_SPRITE", (++current) / totalCount);
    //    AtlasEditorHelper.Build(AtlasEditorHelper.Step.IMPORT_POLY_SPRITE, name, path, PATH_TP_TEMP, PATH_ATLAS_TP, trim, isCompress, isSplitChannel);

    //    AtlasEditorHelper.PlayProgressBar(name, "CREATE_POLY_PREFAB", (++current) / totalCount);
    //    AtlasEditorHelper.Build(AtlasEditorHelper.Step.CREATE_POLY_PREFAB, name, path, PATH_TP_TEMP, PATH_ATLAS_TP, trim, isCompress, isSplitChannel);

    //    AtlasEditorHelper.ClearProgressBar();
    //}
    //private static void ExportAtlas(string path, string name, bool isCompress = true, bool isSplitChannel = true)
    //{
    //    if ((!Config.IsTR && path.Contains("Data_oe")) || (Config.IsTR && !path.Contains("Data_oe")))
    //    {   
    //        Debug.LogError("打图集错误, 尝试把TR(DK)图集打到DK(TR)里,请切换DK/TR工程, 再打图集");
    //        return;
    //    }

    //    BindTpye();

    //    bool trim = true;

    //    int totalCount = 5;
    //    int current = 0;

    //    AtlasEditorHelper.PlayProgressBar(name, "MAKE_ATLAS", (++current) / totalCount);
    //    AtlasEditorHelper.Build(AtlasEditorHelper.Step.MAKE_ATLAS, name, path, PATH_TP_TEMP, PATH_ATLAS_TP, trim, isCompress, isSplitChannel);

    //    AtlasEditorHelper.PlayProgressBar(name, "SPLIT_CHANNEL", (++current) / totalCount);
    //    AtlasEditorHelper.Build(AtlasEditorHelper.Step.SPLIT_CHANNEL, name, path, PATH_TP_TEMP, PATH_ATLAS_TP, trim, isCompress, isSplitChannel);

    //    AtlasEditorHelper.PlayProgressBar(name, "CREATE_MATERIAL", (++current) / totalCount);
    //    AtlasEditorHelper.Build(AtlasEditorHelper.Step.CREATE_MATERIAL, name, path, PATH_TP_TEMP, PATH_ATLAS_TP, trim, isCompress, isSplitChannel);

    //    AtlasEditorHelper.PlayProgressBar(name, "IMPORT_SPRITE", (++current) / totalCount);
    //    AtlasEditorHelper.Build(AtlasEditorHelper.Step.IMPORT_SPRITE, name, path, PATH_TP_TEMP, PATH_ATLAS_TP, trim, isCompress, isSplitChannel);

    //    AtlasEditorHelper.PlayProgressBar(name, "CREATE_PREFAB", (++current) / totalCount);
    //    AtlasEditorHelper.Build(AtlasEditorHelper.Step.CREATE_PREFAB, name, path, PATH_TP_TEMP, PATH_ATLAS_TP, trim, isCompress, isSplitChannel);

    //    AtlasEditorHelper.ClearProgressBar();
    //}
    //private static void ExportAtlasWithoutProgress(string path, string name, bool isCompress = true, bool isSplitChannel = true)
    //{
    //    BindTpye();

    //    bool trim = true;

    //    AtlasEditorHelper.Build(AtlasEditorHelper.Step.MAKE_ATLAS, name, path, PATH_TP_TEMP, PATH_ATLAS_TP, trim, isCompress, isSplitChannel);

    //    AtlasEditorHelper.Build(AtlasEditorHelper.Step.SPLIT_CHANNEL, name, path, PATH_TP_TEMP, PATH_ATLAS_TP, trim, isCompress, isSplitChannel);

    //    AtlasEditorHelper.Build(AtlasEditorHelper.Step.CREATE_MATERIAL, name, path, PATH_TP_TEMP, PATH_ATLAS_TP, trim, isCompress, isSplitChannel);

    //    AtlasEditorHelper.Build(AtlasEditorHelper.Step.IMPORT_SPRITE, name, path, PATH_TP_TEMP, PATH_ATLAS_TP, trim, isCompress, isSplitChannel);

    //    AtlasEditorHelper.Build(AtlasEditorHelper.Step.CREATE_PREFAB, name, path, PATH_TP_TEMP, PATH_ATLAS_TP, trim, isCompress, isSplitChannel);

    //}


    //private static void OptimizingSpineResource(string path)
    //{
    //    BindTpye();

    //    List<string> files = new List<string>();
    //    PdrFileUtil.GetAllChildFiles(path, "mat", files);
    //    foreach (string file in files)
    //    {
    //        Material mat = AssetDatabase.LoadAssetAtPath<Material>(file);
    //        if (!mat.shader.name.Contains("Spine/"))
    //        {
    //            continue;
    //        }
    //        Texture texture = mat.mainTexture;
    //        if (texture == null)
    //        {
    //            continue;
    //        }

    //        string pathTextureFull = AssetDatabase.GetAssetPath(texture);
    //        string pathTexture = pathTextureFull;
    //        int index = pathTexture.LastIndexOf("/");
    //        string name = pathTexture.Substring(index + 1);
    //        name = name.Replace(".png", "");
    //        pathTexture = pathTexture.Substring(0, index);
    //        index = pathTexture.IndexOf("Assets/");
    //        //pathTexture = pathTexture.Substring(index).Replace("Assets/", "");

    //        AtlasEditorHelper.Build(AtlasEditorHelper.Step.SPLIT_CHANNEL, name, pathTexture, pathTexture, pathTexture);
    //        string pathC = string.Format("{0}/{1}_c.png", pathTexture, name);
    //        string pathA = string.Format("{0}/{1}_a.png", pathTexture, name);
    //        Texture texC = AssetDatabase.LoadAssetAtPath<Texture>(pathC);
    //        Texture texA = AssetDatabase.LoadAssetAtPath<Texture>(pathA);

    //        mat.shader = Shader.Find("Custom/Spine/SkeletonGhost");
    //        mat.mainTexture = texC;
    //        mat.SetTexture("_MainTex", texC);
    //        mat.SetTexture("_AlphaTex", texA);

    //        PdrFileUtil.DeleteFile(pathTextureFull);
    //        AssetDatabase.Refresh();
    //    }
    //}
    //private static void BindTpye()
    //{
    //    AtlasEditorHelper.AtlasHierarchy = typeof(AtlasHierarchy);
    //    AtlasEditorHelper.PaddingData = typeof(PaddingData);
    //    AtlasEditorHelper.PandoraAtlasExtends = typeof(PdrAtlasExtends);
    //}
}

